const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'learn_express'
});

// 每个占位符?匹配数据插入
connection.query(
  'insert into user (username,password,status) values (?,?,?)',
  ['用户1', '111', 0],
  (error, results) => {
    if (error) {
      return console.error(error.message);
    }
    
    console.log(results);
    /*OkPacket {
        fieldCount: 0,
        affectedRows: 1, // affectedRows为1表示插入成功
        insertId: 2,
        serverStatus: 2,
        warningCount: 0,
        message: '',
        protocol41: true,
        changedRows: 0
    }*/
  }
);

// 占位符?匹配对象插入，属性名与字段名需完全一致
connection.query(
  'insert into user set ?',
  {
    username: '用户2',
    password: '222',
    status: 1
  },
  (error, results) => {
    if (error) {
      return console.error(error.message);
    }
    
    console.log(results);
    /*OkPacket {
        fieldCount: 0,
        affectedRows: 1, // affectedRows为1表示插入成功
        insertId: 2,
        serverStatus: 2,
        warningCount: 0,
        message: '',
        protocol41: true,
        changedRows: 0
    }*/
  }
);